package com.china08.yxyapi.api.manage.sch.platform.xiaoben.auth;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.service.db.YxyAuthService;

@CrossOrigin
@RestController
public class KechengAutoListController extends YxyApiBaseController {

    @Autowired
    YxyAuthService yxyAuthServiceImpl;

    @Autowired
    YxyLessonsSchoolCatalogRepository schoolCalalogRepository;

    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/auth/kecheng/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        Integer pageNum = reqModel.pageNum;
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String keyword = (StringUtils.isBlank(reqModel.keyword)) ? "" : "%" + reqModel.keyword + "%";
        Integer status = Integer.parseInt(reqModel.status);

        RespModel respModel = new RespModel();

        Integer allSize = 0;
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = yxyAuthServiceImpl.list(schoolId, catalogId1, catalogId2, keyword, status, pageNum);
        if (map != null) {
            allSize = Integer.parseInt(map.get("allSize") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }
        List<Pojo> returnList = new ArrayList<Pojo>();
        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            String id = m.get("id") + "";
            String name = m.get("name") + "";
            String cId1 = m.get("catalog_id1") + "";
            String cId2 = m.get("catalog_id2") + "";
            String count = m.get("count") + "";
            String vip = m.get("vip") + "";

            pojo.id = id;
            pojo.name = name;
            pojo.count = count;

            // 获取一级栏目名称二级栏目名称
            YxyLessonsSchoolCatalog catalog1 = schoolCalalogRepository.findById(cId1);
            YxyLessonsSchoolCatalog catalog2 = schoolCalalogRepository.findById(cId2);
            if (null != catalog1) {
                pojo.catalogName1 = catalog1.getName();
            }
            if (null != catalog2) {
                pojo.catalogName2 = catalog2.getName();
            }
            if (vip.equals("0")) {
                pojo.isVip = "非授权课程";
            }
            if (vip.equals("1")) {
                pojo.isVip = "授权课程";
            }

            // 获取已经授权用户数
            Integer isVipNum = yxyAuthServiceImpl.count(schoolId, id);
            pojo.isVipNum = isVipNum;
            returnList.add(pojo);
        }

        respModel.allSize = allSize;
        respModel.list = returnList;
        return respModel;
    }

    public static class Pojo {
        public String id;
        public String name;
        public String catalogName1;
        public String catalogName2;
        public String isVip;
        public Integer isVipNum;
        public String count;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public Integer pageNum;
        public String catalogId1;
        public String catalogId2;
        public String keyword;
        public String status;
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allSize;
        public List<Pojo> list;
    }
}
